package com.mentaleye.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.StudentsBean;
import com.mentaleye.biz.DictionaryBiz;
import com.mentaleye.biz.OaClassBiz;
import com.mentaleye.biz.StudentsBiz;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;

@Controller
@RequestMapping(value = "/studentsController")
public class StudentsController {

	@Autowired
	private DictionaryBiz dictionaryBiz;
	
	@Autowired private OaClassBiz oaClassBiz;
	
	@Autowired
	private StudentsBiz studentsBiz;
	
	@RequestMapping("/toListPage")
 	public ModelAndView toListPage() {
		ModelAndView view = new ModelAndView("ea/students/studentsList");
		view.addObject("stateList", this.dictionaryBiz.getListByParent(DictionaryEnums.STUDENT_STATE.getCode()));
		return view;
	}

	@RequestMapping("/toDetailPage")
	public ModelAndView toDetailPage(@RequestParam Integer id) {
		ModelAndView view = new ModelAndView("ea/students/studentsDetail");
		view.addObject("student", this.studentsBiz.getStudentMapById(id));
		return view;
	}

	@RequestMapping("/toAddPage")
	public ModelAndView toAddPage() {
		ModelAndView view = new ModelAndView("ea/students/studentsAdd");
		view.addObject("stateList", this.dictionaryBiz.getListByParent(DictionaryEnums.STUDENT_STATE.getCode()));
		view.addObject("sexList", this.dictionaryBiz.getListByParent(DictionaryEnums.SEX.getCode()));
		view.addObject("educationList", this.dictionaryBiz.getListByParent(DictionaryEnums.EDUCATION.getCode()));
		view.addObject("typeList", this.dictionaryBiz.getListByParent(DictionaryEnums.STUDENT_TYPE_TYPE.getCode()));
		return view;
	}

	@RequestMapping("/toEditPage")
	public ModelAndView toEditPage(@RequestParam Integer id) {
		ModelAndView view = new ModelAndView("ea/students/studentsEdit");
		view.addObject("stateList", this.dictionaryBiz.getListByParent(DictionaryEnums.STUDENT_STATE.getCode()));
		view.addObject("sexList", this.dictionaryBiz.getListByParent(DictionaryEnums.SEX.getCode()));
		view.addObject("educationList", this.dictionaryBiz.getListByParent(DictionaryEnums.EDUCATION.getCode()));
		view.addObject("typeList", this.dictionaryBiz.getListByParent(DictionaryEnums.STUDENT_TYPE_TYPE.getCode()));
		view.addObject("students", this.studentsBiz.getStudentMapById(id));
		return view;
	}

	@RequestMapping("/uploadExcel")
	public ModelAndView uploadExcel() {
		ModelAndView view = new ModelAndView("ea/students/uploadExcel");
		return view;
	}
	
	@RequestMapping("/toClassListPage")
	public ModelAndView toClassListPage(@RequestBody List<Integer> idList){
		ModelAndView view=new ModelAndView("ea/students/classList");
		view.addObject("method", "分配班级");
		view.addObject("idList", idList);
		view.addObject("classTypeList" ,dictionaryBiz.getListByParent(DictionaryEnums.CURRICULUM_TYPE.getCode()));
		view.addObject("classStateList",dictionaryBiz.getListByParent(DictionaryEnums.CLASS_STATE.getCode()));
		return view;
	}
	
	@RequestMapping(value = "/oaClassListData")
	@ResponseBody
	public String oaClassListDate(CommonPage commonPage){
		this.oaClassBiz.getListByPage(commonPage);
		return commonPage.toSHowPage();
	}
	
	@RequestMapping("/changeClassListPage")
	public ModelAndView changeClassListPage(@RequestParam Integer id){
		ModelAndView view=new ModelAndView("ea/students/classList");
		view.addObject("method", "转班");
		view.addObject("bean", this.studentsBiz.getStudentMapById(id));
		return view;
	}
	
	@RequestMapping("/toChangeClassPage")
	public ModelAndView toChangeClassPage(@RequestBody Map<String,Object> map){
		ModelAndView view=new ModelAndView("ea/students/changeClass");
		this.oaClassBiz.toChangeClassPage(map);
		view.addObject("bean", map);
		return view;
	}
	
	@RequestMapping("/insertData")
	@ResponseBody
	public String insertData(StudentsBean dataBean) {
		CommonResult result = this.studentsBiz.insertData(dataBean);
		return JSONObject.toJSONString(result);
	}

	@RequestMapping("/updateData")
	@ResponseBody
	public String updateData(StudentsBean dataBean) {
		CommonResult result = this.studentsBiz.updateData(dataBean);
		return JSONObject.toJSONString(result);
	}

	@RequestMapping("/deleteData")
	@ResponseBody
	public String deleteData(@RequestBody List<Integer> idList) {
		CommonResult result = this.studentsBiz.deleteByKeyList(idList);
		return JSONObject.toJSONString(result);
	}

	@RequestMapping("/getList")
	@ResponseBody
	public String getList(CommonPage page) {
		this.studentsBiz.getListByPage(page);
		return page.toSHowPage();
	}

	@RequestMapping("/studentsToClass")
	@ResponseBody
	public String studentsToClass(@RequestBody Map<String,Object> paramMap) {
		CommonResult result = this.studentsBiz.studentsToClass(paramMap);
		return JSONObject.toJSONString(result);
	}

	@RequestMapping("/studentChangeClass")
	@ResponseBody
	public String studentChangeClass(@RequestBody Map<String,Object> paramMap) {
		CommonResult result = this.studentsBiz.studentChangeClass(paramMap);
		return JSONObject.toJSONString(result);
	}

	@RequestMapping("/StudentsDrop")
	@ResponseBody
	public  String StudentsDrop(@RequestParam Integer personId) {
		CommonResult result = this.studentsBiz.StudentsDrop(personId);
		return JSONObject.toJSONString(result);
	}
	
	@RequestMapping(value = "/fileUpload", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public String fileUpload(@RequestParam MultipartFile myfile, HttpServletRequest request) {
		CommonResult result = this.studentsBiz.fileUpload(myfile, request);
		return JSONObject.toJSONString(result);
	}

	@RequestMapping("/excelImport")
	@ResponseBody
	public String excelImport(@RequestBody String filename, HttpServletRequest request) {
		CommonResult result = this.studentsBiz.excelImport(filename, request);
		return JSONObject.toJSONString(result);
	}
}
